//
//  DetailViewModel.swift
//  VO_Yuejie
//
//  Created by laichunhui on 15/11/26.
//  Copyright © 2015年 lch. All rights reserved.
//
//MARK: - 主题视图列表

class PageListViewModel {
    
    lazy var pages = [PageViewModel]()
    
    /// 加载主题详情
    func loadTopicDetail(value: String, finished: (error: NSError?)->()) {

        TopicDAL.getTopicDetail(value) { (array, error) -> () in
            
            if error != nil {
                finished(error: error)
                return
            }
            
            var arrayM = [PageViewModel]()
            
            for dict in array! {
                arrayM.append(PageViewModel(page: DetailPage(dict: dict["data"] as! Dictionary)))
            }

            self.pages = arrayM
            //print(self.pages)
      
            finished(error: nil)
        }
    }
}

//MARK: - 主题详情cell视图模型

class PageViewModel: CustomStringConvertible {
    
    /// 构造函数
    init(page: DetailPage) {
        // print("---")
        self.page = page
    }
    
    /// 书页模型
    var page: DetailPage
    
    /// 配图高度
    lazy var pictureHeight: CGFloat =  {
      return ScreenWidth * (self.page.image_height / self.page.image_width)
    }()
    /// short_content高度
    lazy var shortContentHeight: CGFloat = {
        let nsStr: NSString = NSString(string: self.page.short_content!)
    
        let height: CGFloat = nsStr.boundingRectWithSize(CGSizeMake(ScreenWidth - 20, CGFloat(MAXFLOAT)), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont.systemFontOfSize(12)], context: nil).size.height
        
        return height + 15
    }()
    
    /// page高度
    lazy var rowHeight: CGFloat = {
        
        let titleH: CGFloat = 30
        
        //print(self.page.image_height)
        var rowHeight: CGFloat = 6 + self.pictureHeight + 8 + titleH + 10

        if self.page.short_content != "" {
    
            rowHeight += self.shortContentHeight
        }
        
        return rowHeight
    }()
    
    
    /// 创建时间
    var createAt: String {
        if let date = NSDate.sinaDate(page.created_at ?? "") {
            return date.dateDescription
        }
        return ""
    }
    
    /// 配图 URL
    var image_url: NSURL {
        return NSURL(string: page.image_url_s ?? "")!
    }
    
    /// 标题
    var title: String? {
        return page.text
    }
    
    /// 简内容
    var short_content: String? {
        return page.short_content
    }
    
    /// 页码
    var pageIndex: String = ""
    

    /// 描述信息
    var description: String {
        return page.description
    }
}
